package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.core.MapUntils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpCookie;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class MONITAOSHOUYOUAttr implements AdapterAttr {

    /**
     * 此方法是登录方法，操作下架之前 要先登录
     *
     * @return
     */
    private static String login() {
        long l = System.currentTimeMillis();
        long l2 = l/1000;
        String cookie = "$first_referrer=%u53D6%u503C%u5F02%u5E38; $first_referrer_host=undefined; $first_browser_language=zh-CN; Hm_lvt_3479dfbef7091d5aa569ec56e13113df=1713176033,1714274162; $first_visit_time=1714284658368; _c_WBKFRo=3uC0bZLLYwjo5zYbY6pnjqdyiTiKRIBhdHHbefdw; _csrf=815e62c666d29e7902a77b158f356e134d0265b7d1ccedb4a7b012a98b493ae4a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22Gf1K4OOAdfsqARZPxDPj-rzKldOua3Yo%22%3B%7D; Hm_lvt_417fdf811fee757b9b1f949949acba5c=1735373373; HMACCOUNT=3309EA28EF2272B8; login_info=%7B%22name%22%3A%2218093522409%22%2C%22pwd%22%3A%22Aa120120..%22%2C%22type%22%3A1%7D; TSYUUID=30c25e76-4dc7-4d99-a08b-7406a52dc9fe; PHPSESSID=u57v1813vjlptdav6mekg82222; show_type=1; Hm_lpvt_417fdf811fee757b9b1f949949acba5c=1735543663";
        return cookie;
    }

    public static void main(String[] args) {
        String body = HttpRequest.get("https://www.taoshouyou.com/com/game/game2goodlist?_=1736316116780").execute().body();
        System.out.println(body);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        List<GamesRes> gamesResList = new ArrayList<>();
        String body = HttpRequest.get("https://www.taoshouyou.com/com/game/game2goodlist?_=1736316116780").execute().body();
        JSONArray jsonArray = JSONArray.parseArray(body);
        for(Object o : jsonArray) {
            String result = MapUntils.unicodeToString(o.toString());
            JSONObject jsonObject = JSONObject.parseObject(result);
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("gameid");
            String name = jsonObject.getString("gamename");
            gamesRes.setId(id);
            gamesRes.setGameName(name);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {

        List<InterServerRes> interServerRes = new ArrayList<>();
        InterServerRes interServer = new InterServerRes();
        interServer.setUuid(UUIDGenerator.generateUuid6());
        interServer.setId("区服不匹配!");
        interServer.setName("区服不匹配!");
        interServerRes.add(interServer);
        return interServerRes;
    }

    public String readTxtFile(String html) {

//        ClassPathResource classPathResource = new ClassPathResource("zhuanzhuan.json");

        StringBuffer buffer = new StringBuffer();

//        File file = new File("E:\\img\\zhuan.json");
        File file = new File(html);

        String encoding = "utf-8";
        try (InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
             BufferedReader bufferedReader = new BufferedReader(read)) {
            //判断文件是否存在
            if (file.isFile() && file.exists()) {
                String lineTxt;
                while ((lineTxt = bufferedReader.readLine()) != null) {
//                    System.out.println(lineTxt);
                    buffer.append(lineTxt);
                }
            } else {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }
    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> interAttrResList = new ArrayList<>();
//        String s1 = readTxtFile("G:\\tsyhtml\\tsyyxlmsy.html");
        String s1 = "";
        if(gameId.equals("2256")){
            s1 = readTxtFile("/home/tsywz.html");
        }else if(gameId.equals("2660")){
            s1 = readTxtFile("/home/tsyqzwz.html");
        }else if(gameId.equals("7897")){
            s1 = readTxtFile("/home/tsyyxlm.html");
        }else if(gameId.equals("12170")){
            s1 = readTxtFile("/home/tsyyxlmsy.html");
        }else if(gameId.equals("27226")){
            s1 = readTxtFile("/home/tsyaqtw.html");
        }else if(gameId.equals("22194")){
            s1 = readTxtFile("/home/tsyjcc.html");
        }else if(gameId.equals("22764")){
            s1 = readTxtFile("/home/tsyyjwj.html");
        }else if(gameId.equals("21753")){
            s1 = readTxtFile("/home/tsyyjwjsy.html");
        }else {
            throw new GlobalException("当前游戏暂不支持!");
        }

        Document document = Jsoup.parse(s1);
        Elements elements = document.getElementsByClass("js-b-user-attritem");
        System.out.println(elements);
        if (Objects.equals("1",type)){
            for(Element element : elements){
                Elements elements2 = element.getElementsByAttributeValue("attrtype", "2");
                Elements elements4 = element.getElementsByAttributeValue("attrtype", "4");
                for(Element element2 : elements2){
                    InterAttrRes interAttrRes = new InterAttrRes();
                    interAttrRes.setId(element2.attr("aid"));
                    interAttrRes.setName(onlyHanZi(element2.select("span").get(0).text()));
                    interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                    List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                    String li = element2.select("span").text();
                    String[] inputs = li.split(" ");
                    for (String s : inputs) {
                        InterAttrRes interAttrRes1 = new InterAttrRes();
                        interAttrRes1.setName(s);
                        interAttrRes1.setId(s);
                        interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                        interAttrResList1.add(interAttrRes1);
                    }
                    interAttrRes.setInterServerRes(interAttrResList1);
                    interAttrResList.add(interAttrRes);
                }
                for(Element element2 : elements4){
                    InterAttrRes interAttrRes = new InterAttrRes();
                    interAttrRes.setId(element2.attr("aid"));
                    interAttrRes.setName(onlyHanZi(element2.select("span").get(0).text()));
                    interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                    List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                    String li = element2.select("li").text();
                    String[] inputs = li.split(" ");
                    for (String s : inputs) {
                        InterAttrRes interAttrRes1 = new InterAttrRes();
                        interAttrRes1.setName(s);
                        interAttrRes1.setId(s);
                        interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                        interAttrResList1.add(interAttrRes1);
                    }
                    interAttrRes.setInterServerRes(interAttrResList1);
                    interAttrResList.add(interAttrRes);
                }
            }
        }else if (Objects.equals("2",type)){
            for(Element element : elements){
                Elements elements2 = element.getElementsByAttributeValue("attrtype", "3");
                for(Element element2 : elements2){
                    InterAttrRes interAttrRes = new InterAttrRes();
                    interAttrRes.setId(element2.attr("aid"));
                    interAttrRes.setName(onlyHanZi(element2.select("span").get(0).text()));
                    interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                    List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                    String li = element2.select("span").text();
                    String[] inputs = li.split(" ");
                    for (String s : inputs) {
                        InterAttrRes interAttrRes1 = new InterAttrRes();
                        interAttrRes1.setName(s);
                        interAttrRes1.setId(s);
                        interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                        interAttrResList1.add(interAttrRes1);
                    }
                    interAttrRes.setInterServerRes(interAttrResList1);
                    interAttrResList.add(interAttrRes);
                }
            }
        }else if (Objects.equals("3",type)){
            for(Element element : elements){
                Elements elements2 = element.getElementsByAttributeValue("rel", "must");
                for(Element element2 : elements2){
                    if(element2.attr("attrtype").equals("1")){
                        InterAttrRes interAttrRes = new InterAttrRes();
                        interAttrRes.setId(element2.attr("aid"));
                        interAttrRes.setName(onlyHanZi(element2.select("span").get(0).text()));
                        interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                        List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                        String li = element2.select("li").text();
                        String[] inputs = li.split(" ");
                        for (String s : inputs) {
                            InterAttrRes interAttrRes1 = new InterAttrRes();
                            interAttrRes1.setName(s);
                            interAttrRes1.setId(s);
                            interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                            interAttrResList1.add(interAttrRes1);
                        }
                        interAttrRes.setInterServerRes(interAttrResList1);
                        interAttrResList.add(interAttrRes);
                    }
                }
            }
        }
        return interAttrResList;
    }

    //只保留中文
    private String onlyHanZi(String str) {
        String reg = "[^\u4e00-\u9fa5]";
        str = str.replaceAll(reg, "");
        return str;
    }
}